<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改实体用况')" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-interface-edit" th:object="${virtualUsage}">
            <h4 class="form-header h4">虚拟用况接口信息</h4>
            <input name="id" th:field="*{id}" type="hidden" id="id">
            <div class="form-group">    
                <label class="col-sm-3 control-label">主接口名称：</label>
                <div class="col-sm-8">
                    <input name="masterInterfaceName" th:field="*{masterInterfaceName}" class="form-control" type="text" disabled>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">主接口前后类型：</label>
                <div class="col-sm-8">
                    <select name="masterFbType" class="form-control m-b" th:with="type=${@dict.getType('fbType')}" disabled>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{masterFbType}"></option>
                    </select>
                </div>
            </div>

            <div class="form-group">    
                <label class="col-sm-3 control-label">调用接口名称：</label>
                <div class="col-sm-8">
                    <input name="slaveInterfaceName" th:field="*{slaveInterfaceName}" class="form-control" type="text" disabled>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">调用接口前后类型：</label>
                <div class="col-sm-8">
                    <select name="slaveFbType" class="form-control m-b" th:with="type=${@dict.getType('fbType')}" disabled>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{slaveFbType}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">接口描述：</label>
                <div class="col-sm-8">
                    <textarea name="description" class="form-control">[[*{description}]]</textarea>
                </div>
            </div>

            <h4 class="form-header h4">调用接口入参信息</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="addParam(1)"><i class="fa fa-plus"> 新增参数</i></button>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table-in" style="table-layout:fixed;"></table>
                    </div>
                </div>
            </div>
            <br><br>
            <h4 class="form-header h4">调用接口出参信息</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="addParam(2)"><i class="fa fa-plus"> 新增参数</i></button>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table-out"></table>
                    </div>
                </div>
            </div>
            <input type="hidden" id="interfaceType" name="interfaceType" th:field="*{interfaceType}">
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <script th:inline="javascript">
        var prefix = ctx + "interfaceParamsFieldRelation/relation";
        $("#form-interface-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/saveParam", $('#form-interface-edit').serialize());
            }
        }

        var interfaceType = $("#interfaceType").val();

        $(function() {
            var options1 = {
                id:"bootstrap-table-in",
                code: "id",
                parentCode: "parentId",
                uniqueId: "id",
                expandAll: false,
                expandFirst: false,
                showSearch: false,
                showPageGo: false,
                showRefresh: false,
                showColumns: false,
                url: prefix + "/listParam?interfaceType="+interfaceType+"&paramType=1&interfaceId="+$("#id").val(),
                createUrl: prefix + "/add/{id}",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove/{id}",
                modalName: "入参",
                columns: [{
                    field: 'selectItem',
                    radio: true
                },
                    {
                        title: '参数名称',
                        field: 'fieldName',
                        width: '30',
                        widthUnit: '%',
                        formatter: function(value, row, index) {
                            return row.fieldEnName + "(" + value + ")";
                        }
                    },
                    {
                        field: 'tableName',
                        title: '所属表',
                        width: '30',
                        widthUnit: '%',
                        align: "left",
                        formatter: function(value, row, index) {
                            if (value) {
                                return row.tableEnName+ "(" + value + ")";
                            }
                            return '';
                        }
                    },
                    {
                        title: '字段类型',
                        field: 'fieldType',
                        width: '10',
                        widthUnit: '%',
                        align: "left",
                        formatter: function(value, item, index) {
                            if (item.fieldType == '1') {
                                return '<span class="label label-success">字段</span>';
                            }
                            else if (item.fieldType == '2') {
                                return '<span class="label label-primary">数据集</span>';
                            }
                        }
                    },
                    {
                        field: 'sortNum',
                        title: '排序',
                        width: '10',
                        widthUnit: '%',
                        align: "left"
                    },
                    {
                        title: '操作',
                        width: '20',
                        widthUnit: '%',
                        align: "left",
                        formatter: function(value, row, index) {
                            var actions = [];
                            if (row.fieldType == '2') {
                                actions.push('<a class="btn btn-info btn-xs " href="javascript:void(0)" onclick="addParamsField(\'' + row.id + '\', 1)"><i class="fa fa-plus"></i>新增</a> ');
                            }
                            if (row.parentId == 0) {
                                actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="removeParam(\'' + row.id + '\', 1)"><i class="fa fa-trash"></i>删除</a>');
                            } else {
                                actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="removeParamsField(\'' + row.subId + '\', 1)"><i class="fa fa-trash"></i>删除</a>');
                            }
                            return actions.join('');
                        }
                    }]
            };
            $.treeTable.init(options1);

            var options2 = {
                id:"bootstrap-table-out",
                code: "id",
                parentCode: "parentId",
                uniqueId: "id",
                expandAll: false,
                expandFirst: false,
                showSearch: false,
                showPageGo: false,
                showRefresh: false,
                showColumns: false,
                url: prefix + "/listParam?interfaceType="+interfaceType+"&paramType=2&interfaceId="+$("#id").val(),
                createUrl: prefix + "/add/{id}",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove/{id}",
                modalName: "出参",
                onLoadSuccess:function(){
                    $("#bootstrap-table-out").find("tbody").find("tr").unbind();
                    $("#bootstrap-table-out").find("tbody").find("tr").click(function() {
                        var _isCollapsed = $(this).hasClass("expanderCollapsedClass");
                        if (true) {
                            var tr = $(this);
                            var row_id = tr.attr("id");
                            var _ls = $("#bootstrap-table-out").find("tbody").find("tr[id^='" + row_id + "_']"); //下所有
                            if (_isCollapsed) {
                                $(this).removeClass('expanderCollapsedClass');
                                if (_ls && _ls.length > 0) {
                                    $.each(_ls, function(index, item) {
                                        $(item).css("display", "none");
                                    });
                                }
                            } else {
                                $(this).addClass('expanderCollapsedClass');
                                if (_ls && _ls.length > 0) {
                                    $.each(_ls, function(index, item) {
                                        $(item).css("display", "table");
                                    });
                                }
                            }
                        }
                    });
                },
                columns: [{
                    field: 'selectItem',
                    radio: true
                },
                    {
                        title: '参数名称',
                        field: 'fieldName',
                        width: '30',
                        widthUnit: '%',
                        formatter: function(value, row, index) {
                            return row.fieldEnName+ "(" + value + ")";
                        }
                    },
                    {
                        field: 'tableName',
                        title: '所属表',
                        width: '30',
                        widthUnit: '%',
                        align: "left",
                        formatter: function(value, row, index) {
                            if (value) {
                                return row.tableEnName + "(" + value + ")";
                            }
                            return '';
                        }
                    },
                    {
                        title: '字段类型',
                        field: 'fieldType',
                        width: '10',
                        widthUnit: '%',
                        align: "left",
                        formatter: function(value, item, index) {
                            if (value == '1') {
                                return '<span class="label label-success">字段</span>';
                            }
                            else if (value == '2') {
                                return '<span class="label label-primary">数据集</span>';
                            }
                        }
                    },
                    {
                        field: 'sortNum',
                        title: '排序',
                        width: '10',
                        widthUnit: '%',
                        align: "left"
                    },
                    {
                        title: '操作',
                        width: '20',
                        widthUnit: '%',
                        align: "left",
                        formatter: function(value, row, index) {
                            var actions = [];
                            if (row.fieldType == '2') {
                                actions.push('<a class="btn btn-info btn-xs " href="javascript:void(0)" onclick="addParamsField(\'' + row.id + '\', 2)"><i class="fa fa-plus"></i>新增</a> ');
                            }
                            if (row.parentId == 0) {
                                actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="removeParam(\'' + row.id + '\', 2)"><i class="fa fa-trash"></i>删除</a>');
                            } else {
                                actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="removeParamsField(\'' + row.subId + '\', 2)"><i class="fa fa-trash"></i>删除</a>');
                            }
                            return actions.join('');
                        }
                    }]
            };
            $.treeTable.init(options2);
        });

        /**
         * 添加字段或者参数集
         */
        function addParam(isInParam) {
            table.set();
            $.modal.open("添加虚拟接口字段或者参数集", prefix + "/addVU?type="+interfaceType+"&isInParam=" + isInParam + "&interfaceId=" + $("#id").val(), $(window).width() - 50);
        }

        /**
         * 添加参数集下字段
         */
        function addParamsField(id, isInParam) {
            table.set();
            $.modal.open("添加虚拟接口参数集下字段", prefix + "/addVUParamsField?type="+interfaceType+"&isInParam=" + isInParam + "&interfaceDataListId=" + id, $(window).width() - 50);
        }

        /**
         * 删除参数集下字段
         * @param id
         */
        function removeParam(id, isInParam) {
            table.set();
            var modalName = "入参";
            if (isInParam == 2) {
                modalName = "出参";
            }
            var url = prefix + '/remove/' + id;
            var p = [];
            p.push(id)
            $.modal.confirm("确定删除该条" + modalName + "信息吗？", function() {
                var config = {
                    url: url,
                    type: "get",
                    dataType: "json",
                    data: null,
                    success: function(result) {
                        if (result.code == web_status.SUCCESS) {
                            if (isInParam == 2) {
                                $("#bootstrap-table-out").bootstrapTreeTable('refresh');
                            } else {
                                $("#bootstrap-table-in").bootstrapTreeTable('refresh');
                            }
                        } else {
                            $.modal.alertWarning(result.msg);
                        }
                    }
                };
                $.ajax(config)
            });
        }
        var prefix2 = ctx + "paramsFieldRelation/parameter";
        /**
         * 删除参数集下字段
         * @param id
         */
        function removeParamsField(id, isInParam) {
            table.set();
            var modalName = "入参";
            if (isInParam == 2) {
                modalName = "出参";
            }
            $.modal.confirm("确定删除该条" + modalName + "信息吗？", function() {
                var config = {
                    url: prefix2 + '/remove/'+id,
                    type: "get",
                    dataType: "json",
                    data: null,
                    success: function(result) {
                        if (result.code == web_status.SUCCESS) {
                            if (isInParam == 2) {
                                $("#bootstrap-table-out").bootstrapTreeTable('refresh');
                            } else {
                                $("#bootstrap-table-in").bootstrapTreeTable('refresh');
                            }
                        } else {
                            $.modal.alertWarning(result.msg);
                        }
                    }
                };
                $.ajax(config);
            });
        }
    </script>
</body>
</html>